package com.thinkit.bigdata.web.controller.pubmet;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.thinkit.bigdata.core.feature.orm.mybatis.Page;
import com.thinkit.bigdata.web.model.BaseEntity;
import com.thinkit.bigdata.web.model.config.SysArea;
import com.thinkit.bigdata.web.model.config.SysAreaExample;
import com.thinkit.bigdata.web.model.es.audioEs.RuleInfos;
import com.thinkit.bigdata.web.service.config.AreaService;
import org.apache.shiro.SecurityUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;

import java.util.*;

public class PubMethod {

    //获取当前登陆用户满足的省份code
    public static Map<String, List<String>> getProList(AreaService areaService, Integer pageNum, Integer pageSize) {
        Map<String, List<String>> map = new HashMap<>();
        List<String> queryList = new ArrayList<>();
        SysArea areaInfo = (SysArea) SecurityUtils.getSubject().getSession().getAttribute("areaInfo");
        if(areaInfo == null ||areaInfo.getId() == null){
            List<SysArea> areas;
            SysAreaExample example = new SysAreaExample();
            example.createCriteria().andEnabledEqualTo(BaseEntity.AREA_ENABLEE_YES);
            //集团人员
            if (pageNum != null) {
                Page<SysArea> page = new Page<>(pageNum + 1, pageSize);
                areas = areaService.selectByExample(page, example);
                map.put("count", Collections.singletonList(page.getTotalCount() + ""));
            } else {
                areas = areaService.selectByExample(example);
                map.put("count", Collections.singletonList(areas.size() + ""));
            }
            for (SysArea area : areas) {
                queryList.add(area.getCode());
            }
        }else{
            queryList.add(areaInfo.getCode());
            map.put("count", Collections.singletonList("1"));
        }
        map.put("list", queryList);
        return map;
    }

    //根据规则id获取规则命中量
    public static Integer getRuleTimeByRuleId(List<RuleInfos> ruleInfos, String ruleId) {
        for (RuleInfos info : ruleInfos) {
            if (ruleId.equals(info.getRuleId().toString())) {
                return info.getRuleTimes();
            }
        }
        return 0;
    }

    //省份，城市  多选
    public static void formatPro(String provinceCode, BoolQueryBuilder queryBuilder) {
        Map<String, List<String>> proMap = strToJson(provinceCode);
        BoolQueryBuilder shuldQ = new BoolQueryBuilder();
        for (String code : proMap.keySet()) {
            BoolQueryBuilder mustQ = new BoolQueryBuilder();
            mustQ.must(QueryBuilders.matchQuery("voiceInfo.provinceCode", code));
            //归属地为空时，为直辖市
            formatPC(mustQ, proMap.get(code));
            shuldQ.should(mustQ);
        }
        queryBuilder.must(shuldQ);
    }

    public static void formatPC(BoolQueryBuilder mustQ, List<String> cityS) {
        //归属地为空时，为直辖市
        if (!cityS.isEmpty()) {
            BoolQueryBuilder citySQ = new BoolQueryBuilder();
            for (String city : cityS) {
                citySQ.should(QueryBuilders.matchQuery("voiceInfo.customerloc", city));
            }
            mustQ.must(citySQ);
        }
    }

    public static void formatOnHook(BoolQueryBuilder mustQ, List<String> onHooks) {
        //归属地为空时，为直辖市
        if (!onHooks.isEmpty()) {
            BoolQueryBuilder citySQ = new BoolQueryBuilder();
            for (String onHook : onHooks) {
                citySQ.should(QueryBuilders.matchQuery("voiceInfo.onHook", onHook));
            }
            mustQ.must(citySQ);
        }
    }

    public static void formatCustomerLevel(BoolQueryBuilder boolQuery, List<String> customerLevels) {
        //客户星级
        BoolQueryBuilder shuldQ = new BoolQueryBuilder();
        for (String customerLevel : customerLevels) {
            shuldQ.should(QueryBuilders.matchQuery("voiceInfo.customerLevel", customerLevel));
        }
        boolQuery.must(shuldQ);
    }

    public static void formatSatisfactions(BoolQueryBuilder boolQuery, List<String> satisfactions) {
        //满意度
        BoolQueryBuilder shuldQ = new BoolQueryBuilder();
        for (String satisfaction : satisfactions) {
            shuldQ.should(QueryBuilders.matchQuery("voiceInfo.satisfaction", satisfaction));
        }
        boolQuery.must(shuldQ);
    }

    public static void formatSysTagFlags(BoolQueryBuilder boolQuery, List<String> sysTagFlags) {
        //满意度
        BoolQueryBuilder shuldQ = new BoolQueryBuilder();
        for (String sysTagFlag : sysTagFlags) {
            shuldQ.should(QueryBuilders.matchQuery("sysTagFlag", sysTagFlag));
        }
        boolQuery.must(shuldQ);
    }

    public static void formatAppealFlags(BoolQueryBuilder boolQuery, List<String> appealFlags) {
        //满意度
        BoolQueryBuilder shuldQ = new BoolQueryBuilder();
        for (String appealFlag : appealFlags) {
            shuldQ.should(QueryBuilders.matchQuery("appealFlag", appealFlag));
        }
        boolQuery.must(shuldQ);
    }

    public static void formatTime(BoolQueryBuilder queryBuilder, String callStartTime, String callEndTime) {
        //归属地为空时，为直辖市
        if (!Strings.isNullOrEmpty(callStartTime)) {
            RangeQueryBuilder rangeQuery = new RangeQueryBuilder("voiceInfo.callStartTime");
            rangeQuery.gte(callStartTime);
            if (!Strings.isNullOrEmpty(callEndTime)) rangeQuery.lte(callEndTime);
            queryBuilder.must(rangeQuery);
        }
    }

    public static Map<String, List<String>> strToJson(String str) {
        return (Map<String, List<String>>) JSON.parse(str);
    }


}
